package net.cmkj.mine.conf;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import net.cmkj.mine.bean.SysUser;
import net.cmkj.mine.common.taglib.ButtonTagHtml;
import net.cmkj.mine.common.taglib.MenuTagHtml;
import net.cmkj.mine.handler.GlobalHandler;
import net.cmkj.mine.interceptor.AuthorityInterceptor;
import net.cmkj.mine.interceptor.ExceptionInterceptor;
import net.cmkj.mine.interceptor.SessionInterceptor;
import net.cmkj.mine.interceptor.SysLogInterceptor;
import net.cmkj.mine.util.IWebUtils;
import net.cmkj.mine.util.tpp.coder.MappingKit;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal3.JFinal3BeetlRenderFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class MineConfig extends JFinalConfig {

	Routes routes;
	
	public static void main(String[] args) {
		UndertowServer.create(MineConfig.class).addHotSwapClassPrefix("org.beetl.").start();
		//UndertowServer.start(WebConfig.class);
	}

	@Override
	public void configConstant(Constants me) {
		//引用数据库文件
		PropKit.use("db_config.txt");
		
		//模版配置
		JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
		rf.config();
		me.setRenderFactory(rf);
		GroupTemplate gt = rf.groupTemplate;
		gt.registerTag("menu", MenuTagHtml.class);
		gt.registerTag("button", ButtonTagHtml.class);
		Map<String,Object> shared = new HashMap<String,Object>();
		shared.put("name", "beetl");
		gt.setSharedVars(shared);
		
		//添加依赖注入支持
		me.setInjectDependency(true);
		
		//页面属性配置
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setViewType(ViewType.JSP);
		me.setEncoding("UTF-8");
		
		//错误页面配置
		me.setError404View("/page/404.html");
		me.setError500View("/page/500.html");
		me.setErrorView(401, "/page/401.html");
		me.setErrorView(403, "/page/403.html");

		//修改文件上传大小限制100M  100*1024*1024
		me.setMaxPostSize(104857600);
	}

	@Override
	public void configRoute(Routes me) {
		this.routes = me;
		me.add(new MineRoutes());
	}

	@Override
	public void configEngine(Engine me) {
		
	}

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setShowSql(PropKit.getBoolean("devMode", false));
		arp.setDevMode(PropKit.getBoolean("devMode", false));
		me.add(arp);
		
		// 所有配置在 MappingKit 中设置
		MappingKit.mapping(arp);
		
		me.add(new EhCachePlugin());
		//添加redis，用来缓存前台信息
		String RedisHost = PropKit.use("redis.txt").get("host");
		RedisPlugin filesRedis = new RedisPlugin("files", RedisHost);
		me.add(filesRedis);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.addGlobalActionInterceptor(new SysLogInterceptor());
		me.addGlobalActionInterceptor(new AuthorityInterceptor());
		me.addGlobalActionInterceptor(new ExceptionInterceptor());
		me.addGlobalActionInterceptor(new SessionInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new UrlSkipHandler("\\.css$|\\.js$|\\.jpg$|\\.gif$|\\.png$|\\.xml$|\\.swf$|\\.doc$|\\.docx$|\\.xls$|\\.xlsx", false));
		DruidStatViewHandler dvh = new DruidStatViewHandler("/druid", new IDruidStatViewAuth() {
			public boolean isPermitted(HttpServletRequest request) {
				SysUser sysUser = IWebUtils.getCurrentSysUser(request);
				return (sysUser != null && sysUser.getStr("name").equals("admin"));
			}
		});
		me.add(dvh);
		
		me.add(new UrlSkipHandler("^/monitoring", true));

		me.add(new GlobalHandler());
	}
	
	public static DruidPlugin createDruidPlugin() {
		Prop p = PropKit.use("db_config.txt");
		DruidPlugin dp = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
		dp.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType("mysql");
		dp.addFilter(wall);
		return dp;
	}

}
